import Vuex from 'vuex'
import state from './state/state'
import mutations from './mutations/mutations'
import getters from './getters/getters'
import actions from './actions/actions'
const isDev = process.env.NODE_ENV === 'development'

export default () => {
    const store = new Vuex.Store({
        strict: isDev,
        state,
        mutations,
        getters,
        actions,
        modules: {
            a: {
                // 可以有相同的mutations
                namespaced: true,
                state: {
                    text: 1
                },
                mutations: {
                    updateText(state, text){
                        console.log('a.state', state)
                        state.text = text
                    }
                },
                getters: {
                    textPlus (state, getters, rootState) {
                        return state.text+rootState.count
                    }
                },
                actions: {
                    add({state, commit, rootState}){
                        // commit('updateText', rootState.b.text)
                        commit('updateCount', 56789, {root: true})
                    }
                }
            },
            b: {
                state: {
                    text: 200
                },
                actions: {
                    testAction({commit}){
                        commit('a/updateText', 'test text')
                    }
                }
            }
        }
    })

    if (module.hot){
        module.hot.accept([
            './state/state',
            './mutations/mutations',
            './getters/getters',
            './actions/actions'
        ], ()=>{
            const newState = require('./state/state').default
            const newMutations = require('./mutations/mutations').default
            const newGetters = require('./getters/getters').default
            const newActions = require('./actions/actions').default

            store.hotUpdate({
                state: newState,
                mutations: newMutations,
                getters: newGetters,
                actions: newActions
            })
        })
    }
    return store
}

